//
//  WBWelcomeViewController.swift
//  SinaWeibo
//
//  Created by 风不会停息 on 2016/11/17.
//  Copyright © 2016年 风不会停息. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

let bottomMargin:CGFloat = 120
class WBWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        self.view.backgroundColor = UIColor.white
        // Do any additional setup after loading the view.
    }
    
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAniamtion()
    }
    
    
    private lazy var imageVIcon = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
    private lazy var welcomeLabel = UILabel(title: "欢迎回来", fontSize: 16, textcolor: #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1))
    
    
    
    private func setUI() {
        
        view.addSubview(imageVIcon)
        view.addSubview(welcomeLabel)
        
        imageVIcon.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(bottomMargin)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(imageVIcon.snp.bottom).offset(20)
        }
        welcomeLabel.alpha = 0
        imageVIcon.layer.cornerRadius = 85 * 0.5
        imageVIcon.layer.masksToBounds = true
        imageVIcon.sd_setImage(with: WBUserAccountViewModel.WBUserAccountShared.headURL)
    }
    
    private func startAniamtion(){
        
        let offsetY = -(UIScreen.main.bounds.height - bottomMargin - imageVIcon.bounds.height)
        
        
        //更新约束只是收集了约束的变化
        //自动布局是间接的修改frame，在viewdidlayoutSubView中生效！！！！！！！！！
        self.imageVIcon.snp.updateConstraints({ (make) in
            make.bottom.equalTo(self.view).offset(offsetY)
        })
        
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 7, options: [], animations: {
       
            //提前刷新界面
            self.view.layoutIfNeeded()
            
            
        }) { (finished) -> () in
            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (_) ->() in
                
                // 底部的东西销毁掉
                
                
                // 销毁完欢迎界面之后， 将 home页面 换成tableVIew。
                // 没有登录 不能调用这个方法

                NotificationCenter.default.post(name: NSNotification.Name(kChoseRootViewController), object: nil)
////
            })
        }
    }
    
}
